package app;

import java.awt.event.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;

//import java.lang.ClassLoader;

import javax.swing.*;
import framework.DataBaseUtils;

public class MainGuiRegistrationForm extends JFrame {
	//private static ClassLoader cLoader = new ClassLoader() {};

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static JFrame mainFrame;

	/**
	 * Creates new form ContactEditor
	 */
	public MainGuiRegistrationForm() {
		initComponents();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {
		//test

		registerResultFrame = new javax.swing.JFrame();
		registerResultPanel = new javax.swing.JPanel();
		registrationSuccessLabel = new javax.swing.JLabel();
		okSuccessButton = new javax.swing.JButton();
		infoResultLabel = new javax.swing.JLabel();
		infoResultFullNameLabel = new javax.swing.JLabel();
		infoResultLoginLabel = new javax.swing.JLabel();
		infoResultPasswordLabel = new javax.swing.JLabel();
		orgDataPanel = new javax.swing.JPanel();
		fullNameLabel = new javax.swing.JLabel();
		emailLabel = new javax.swing.JLabel();
		fullNameTextField = new javax.swing.JTextField();
		emailTextField = new javax.swing.JTextField();
		phoneLabel = new javax.swing.JLabel();
		phoneTextField = new javax.swing.JTextField();
		ogrnLabel = new javax.swing.JLabel();
		ogrnTextField = new javax.swing.JTextField();
		innLabel = new javax.swing.JLabel();
		innTextField = new javax.swing.JTextField();
		kppLabel = new javax.swing.JLabel();
		kppTextField = new javax.swing.JTextField();
		checkOrgDataButton = new javax.swing.JButton();
		isOgrnUniqueLabel = new javax.swing.JLabel();
		isKppUniqueLabel = new javax.swing.JLabel();
		isInnUniqueLabel = new javax.swing.JLabel();
		userDataPanel = new javax.swing.JPanel();
		loginLabel = new javax.swing.JLabel();
		firstNameLabel = new javax.swing.JLabel();
		loginTextField = new javax.swing.JTextField();
		firstNameTextField = new javax.swing.JTextField();
		lastNameLabel = new javax.swing.JLabel();
		lastNameTextField = new javax.swing.JTextField();
		middleNameLabel = new javax.swing.JLabel();
		middleNameTextField = new javax.swing.JTextField();
		checkUserDataButton = new javax.swing.JButton();
		clearFormButon = new javax.swing.JButton();
		cancelButton = new javax.swing.JButton();
		registerButton = new javax.swing.JButton();
		isLoginUniqueLabel = new javax.swing.JLabel();

		registerResultFrame
				.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		registerResultFrame.setCursor(new java.awt.Cursor(
				java.awt.Cursor.DEFAULT_CURSOR));
		registerResultFrame.setForeground(java.awt.Color.white);
		registerResultFrame.setMinimumSize(new java.awt.Dimension(450, 250));
		registerResultFrame.setLocationRelativeTo(getParent());
		registerResultFrame.setTitle("Результат регистрации");

		registerResultPanel.setMinimumSize(new java.awt.Dimension(200, 300));

		infoResultLabel
				.setText("Организация была зарегистрирована со следующими параметрами:");

		infoResultFullNameLabel.setText("Полное наименование:");

		infoResultLoginLabel.setText("Логин:");

		okSuccessButton.setText("ОК");
		okSuccessButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okSuccessButtonActionPerformed(evt);
			}
		});

		infoResultPasswordLabel.setText("Пароль:");

		registrationSuccessLabel.setFont(new java.awt.Font("Tahoma", 3, 14)); // NOI18N
		registrationSuccessLabel.setText("Регистрация успешно завершена!");
		registrationSuccessLabel.setForeground(OK_LABEL_COLOR);

		org.jdesktop.layout.GroupLayout registerResultPanelLayout = new org.jdesktop.layout.GroupLayout(
				registerResultPanel);
		registerResultPanel.setLayout(registerResultPanelLayout);
		registerResultPanelLayout
				.setHorizontalGroup(registerResultPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(registerResultPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.add(registerResultPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(org.jdesktop.layout.GroupLayout.TRAILING,
												registerResultPanelLayout
														.createSequentialGroup()
														.add(registrationSuccessLabel,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																270,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(52, 52, 52))
										.add(registerResultPanelLayout
												.createSequentialGroup()
												.add(registerResultPanelLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(infoResultLabel)
														.add(registerResultPanelLayout
																.createSequentialGroup()
																.add(29, 29, 29)
																.add(registerResultPanelLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(infoResultFullNameLabel)
																		.add(org.jdesktop.layout.GroupLayout.LEADING,
																				infoResultLoginLabel)
																		.add(org.jdesktop.layout.GroupLayout.LEADING,
																				infoResultPasswordLabel)))
														.add(org.jdesktop.layout.GroupLayout.TRAILING,
																registerResultPanelLayout
																		.createSequentialGroup()
																		.add(okSuccessButton,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				83,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(126,
																				126,
																				126)))
												.addContainerGap()))));
		registerResultPanelLayout.setVerticalGroup(registerResultPanelLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultPanelLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registrationSuccessLabel)
						.add(11, 11, 11)
						.add(infoResultLabel)
						.add(18, 18, 18)
						.add(infoResultFullNameLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(infoResultLoginLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED)
						.add(infoResultPasswordLabel,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addPreferredGap(
								org.jdesktop.layout.LayoutStyle.RELATED,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).add(okSuccessButton)
						.addContainerGap()));

		org.jdesktop.layout.GroupLayout registerResultFrameLayout = new org.jdesktop.layout.GroupLayout(
				registerResultFrame.getContentPane());
		registerResultFrame.getContentPane().setLayout(
				registerResultFrameLayout);
		registerResultFrameLayout.setHorizontalGroup(registerResultFrameLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultFrameLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registerResultPanel,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addContainerGap()));
		registerResultFrameLayout.setVerticalGroup(registerResultFrameLayout
				.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
				.add(registerResultFrameLayout
						.createSequentialGroup()
						.addContainerGap()
						.add(registerResultPanel,
								org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
								170, Short.MAX_VALUE)));

		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setTitle("Форма ввода данных регистрируемой организации");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setMaximumSize(new java.awt.Dimension(710, 318));
		setPreferredSize(new java.awt.Dimension(720, 510));

		getContentPane().setLayout(
				new org.netbeans.lib.awtextra.AbsoluteLayout());

		orgDataPanel.setBorder(BorderFactory
				.createTitledBorder("Данные организации "));
		orgDataPanel.setPreferredSize(new java.awt.Dimension(700, 180));

		fullNameLabel.setText("Полное наименование");

		emailLabel.setText("Email");

		fullNameTextField.setText("My_AUTO test 01");
		fullNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				fullNameTextFieldActionPerformed(evt);
			}
		});

		emailTextField.setText("testmailfz223@gmail.com");
		emailTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				emailTextFieldActionPerformed(evt);
			}
		});

		phoneLabel.setText("Телефон");

		phoneTextField.setText("8-495-9938028");

		ogrnLabel.setText("ОГРН");

		ogrnTextField.setText("9999999999993");
		ogrnTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				ogrnTextFieldActionPerformed(evt);
			}
		});

		innLabel.setText("ИНН");

		innTextField.setText("4444444493");
		innTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				innTextFieldActionPerformed(evt);
			}
		});

		kppLabel.setText("КПП");

		kppTextField.setText("444444493");

		checkOrgDataButton.setText("Проверить уникальность ОГРН+ИНН+КПП");
		checkOrgDataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				checkOrgDataButtonActionPerformed(evt);
			}
		});

		isOgrnUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isOgrnUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isOgrnUniqueLabel.setText("isOrgnUniqueLabel TEXT");
		isOgrnUniqueLabel.setVisible(false);
		isOgrnUniqueLabel.setName("ОГРН");

		isKppUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isKppUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isKppUniqueLabel.setText("isKppUniqueLabel TEXT");
		isKppUniqueLabel.setVisible(false);
		isKppUniqueLabel.setName("КПП");

		isInnUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isInnUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isInnUniqueLabel.setText("isInnUniqueLabel TEXT");
		isInnUniqueLabel.setVisible(false);
		isInnUniqueLabel.setName("ИНН");

		org.jdesktop.layout.GroupLayout orgDataPanelLayout = new org.jdesktop.layout.GroupLayout(
				orgDataPanel);
		orgDataPanel.setLayout(orgDataPanelLayout);
		orgDataPanelLayout
				.setHorizontalGroup(orgDataPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(orgDataPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.TRAILING)
										.add(orgDataPanelLayout
												.createParallelGroup(
														org.jdesktop.layout.GroupLayout.LEADING)
												.add(fullNameLabel)
												.add(org.jdesktop.layout.GroupLayout.TRAILING,
														ogrnLabel)
												.add(org.jdesktop.layout.GroupLayout.TRAILING,
														innLabel))
										.add(kppLabel).add(phoneLabel))
								.add(18, 18, 18)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(orgDataPanelLayout
												.createSequentialGroup()
												.add(orgDataPanelLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(fullNameTextField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																218,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
														.add(org.jdesktop.layout.GroupLayout.TRAILING,
																ogrnTextField,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																218,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
												.add(38, 38, 38)
												.add(emailLabel))
										.add(kppTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												218,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(innTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												218,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(phoneTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												218,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(checkOrgDataButton,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												218,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(isKppUniqueLabel,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.add(isOgrnUniqueLabel,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.add(emailTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												218,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(isInnUniqueLabel,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE))
								.addContainerGap(44, Short.MAX_VALUE)));
		orgDataPanelLayout
				.setVerticalGroup(orgDataPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(orgDataPanelLayout
								.createSequentialGroup()
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(fullNameLabel)
										.add(emailTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(fullNameTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(emailLabel))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(ogrnTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(ogrnLabel).add(isOgrnUniqueLabel))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED,
										28, Short.MAX_VALUE)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(kppTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(kppLabel).add(isKppUniqueLabel))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(innTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(innLabel).add(isInnUniqueLabel))
								.add(7, 7, 7)
								.add(orgDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(phoneTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(phoneLabel)
										.add(checkOrgDataButton,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))));

		getContentPane().add(
				orgDataPanel,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1,
						220));

		userDataPanel.setBorder(BorderFactory
				.createTitledBorder("Данные администратора организации "));

		loginLabel.setText("Логин");

		firstNameLabel.setText("Имя");
		firstNameLabel.setHorizontalTextPosition(SwingConstants.RIGHT);
		firstNameLabel.setInheritsPopupMenu(false);

		loginTextField.setText("223-223-223 55");
		loginTextField.setPreferredSize(new java.awt.Dimension(84, 20));
		loginTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				loginTextFieldActionPerformed(evt);
			}
		});

		firstNameTextField.setText("MY_Имя-Тест");
		firstNameTextField.setPreferredSize(new java.awt.Dimension(84, 20));
		firstNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				firstNameTextFieldActionPerformed(evt);
			}
		});

		lastNameLabel.setText("Фамилия");

		lastNameTextField.setText("MY_Фамилия-Тест");
		lastNameTextField.setPreferredSize(new java.awt.Dimension(84, 20));

		middleNameLabel.setLabelFor(middleNameTextField);
		middleNameLabel.setText("Отчество");
		middleNameLabel.setHorizontalTextPosition(SwingConstants.RIGHT);

		middleNameTextField.setText("MY_Отчество-Тест");
		middleNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				middleNameTextFieldActionPerformed(evt);
			}
		});

		checkUserDataButton.setText("Проверить уникальность логина");
		checkUserDataButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				checkUserDataButtonActionPerformed(evt);
			}
		});

		clearFormButon.setText("Очистить форму");
		clearFormButon.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				clearFormButonActionPerformed(evt);
			}
		});

		cancelButton.setText("Отмена");
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed(evt);
			}
		});

		registerButton.setText("Зарегистрировать");
		registerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				registerButtonActionPerformed(evt);
			}
		});

		isLoginUniqueLabel.setFont(new java.awt.Font("Tahoma", 3, 11)); // NOI18N
		isLoginUniqueLabel.setForeground(new java.awt.Color(255, 51, 51));
		isLoginUniqueLabel.setText("isUserUniqueLabel TEXT");
		isLoginUniqueLabel.setVisible(false);
		isLoginUniqueLabel.setName("Логин");

		org.jdesktop.layout.GroupLayout userDataPanelLayout = new org.jdesktop.layout.GroupLayout(
				userDataPanel);
		userDataPanel.setLayout(userDataPanelLayout);
		userDataPanelLayout
				.setHorizontalGroup(userDataPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(userDataPanelLayout
								.createSequentialGroup()
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.LEADING)
										.add(userDataPanelLayout
												.createSequentialGroup()
												.add(83, 83, 83)
												.add(userDataPanelLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(userDataPanelLayout
																.createSequentialGroup()
																.add(userDataPanelLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(userDataPanelLayout
																				.createParallelGroup(
																						org.jdesktop.layout.GroupLayout.LEADING)
																				.add(lastNameLabel)
																				.add(org.jdesktop.layout.GroupLayout.TRAILING,
																						firstNameLabel,
																						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																						29,
																						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
																		.add(org.jdesktop.layout.GroupLayout.TRAILING,
																				loginLabel))
																.add(18, 18, 18)
																.add(userDataPanelLayout
																		.createParallelGroup(
																				org.jdesktop.layout.GroupLayout.LEADING)
																		.add(userDataPanelLayout
																				.createParallelGroup(
																						org.jdesktop.layout.GroupLayout.LEADING,
																						false)
																				.add(loginTextField,
																						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																						org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																						Short.MAX_VALUE)
																				.add(lastNameTextField,
																						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																						218,
																						org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
																		.add(middleNameTextField,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				218,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																		.add(firstNameTextField,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				218,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
														.add(middleNameLabel,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																59,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
												.add(77, 77, 77)
												.add(userDataPanelLayout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(checkUserDataButton,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.add(userDataPanelLayout
																.createSequentialGroup()
																.add(isLoginUniqueLabel,
																		org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																		218,
																		org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
																.add(0,
																		20,
																		Short.MAX_VALUE))))
										.add(userDataPanelLayout
												.createSequentialGroup()
												.add(253, 253, 253)
												.add(registerButton)
												.add(41, 41, 41)
												.add(cancelButton)
												.add(33, 33, 33)
												.add(clearFormButon,
														org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
														org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)))
								.addContainerGap()));
		userDataPanelLayout
				.setVerticalGroup(userDataPanelLayout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(userDataPanelLayout
								.createSequentialGroup()
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(loginLabel)
										.add(loginTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(isLoginUniqueLabel))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED)
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(lastNameLabel)
										.add(lastNameTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(checkUserDataButton,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED,
										org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(firstNameLabel)
										.add(firstNameTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										org.jdesktop.layout.LayoutStyle.RELATED)
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(middleNameTextField,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(middleNameLabel))
								.add(18, 18, 18)
								.add(userDataPanelLayout
										.createParallelGroup(
												org.jdesktop.layout.GroupLayout.BASELINE)
										.add(registerButton,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(cancelButton,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
										.add(clearFormButon,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
												30,
												org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
								.add(46, 46, 46)));
		getContentPane().add(
				userDataPanel,
				new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 240, 700,
						230));

		getAccessibleContext().setAccessibleName("RegisterOrganizationForm");

		// Рисуем всплывающее окно об успешной регистрации

		okSuccessButton.setText("Ок");
		okSuccessButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				okSuccessButtonActionPerformed(evt);
			}
		});

		infoResultLabel
				.setText("Организация была зарегистрирована со следующими параметрами:");

		infoResultFullNameLabel.setText("Полное наименование:");

		infoResultLoginLabel.setText("Логин:");

		infoResultPasswordLabel.setText("Пароль:");

		pack();

	}// </editor-fold>

	protected void okSuccessButtonActionPerformed(ActionEvent evt) {
		registerResultFrame.dispose();

	}

	private void checkOrgDataButtonActionPerformed(ActionEvent evt) {
		initializeInputData();
		DataBaseUtils.checkOrganizationIsUnique(orgFullName, orgOgrn, orgKpp,
				orgInn);
		displayCheckResults(DataBaseUtils.OGRN_CHECK_RESULT, isOgrnUniqueLabel);
		displayCheckResults(DataBaseUtils.INN_CHECK_RESULT, isInnUniqueLabel);
		displayCheckResults(DataBaseUtils.KPP_CHECK_RESULT, isKppUniqueLabel);

	}

	private void checkUserDataButtonActionPerformed(ActionEvent evt) {
		initializeInputData();
		DataBaseUtils.checkUserIsUnique(userLogin);
		displayCheckResults(DataBaseUtils.LOGIN_CHECK_RESULT,
				isLoginUniqueLabel);
	}

	private void registerButtonActionPerformed(ActionEvent evt) {
		
		
	// инициализирует данные из формы 
		initializeInputData(); 
		// вставляет адрес организации 
		db.insertOrgAddress(); 
		// получаем ИД  вставленного адреса по зип_коду 
		orgAddressId = DataBaseUtils.getAddressId(DataBaseUtils.ZIP_CODE_REPLACEMENT); 
		// вставляеторганизацию 
		db.insertOrganization(orgFullName, orgOgrn,
		  orgInn, orgKpp, orgAddressId, orgPhone, orgWebsite, orgEmail); 
		// вставляет пользователя 
		db.insertUser(userLogin,
		  userFirstName, userMiddleName, userLastName, orgFullName, orgOgrn);
		 
		displayResultWindow();
		
	


	}

	private void initializeInputData() {
		orgFullName = fullNameTextField.getText();
		orgEmail = emailTextField.getText();
		orgKpp = kppTextField.getText();
		orgOgrn = ogrnTextField.getText();
		orgInn = innTextField.getText();
		orgPhone = phoneTextField.getText();
		orgWebsite = "www.test.com";
		userLogin = loginTextField.getText();
		userFirstName = firstNameTextField.getText();
		userLastName = lastNameTextField.getText();
		userMiddleName = middleNameTextField.getText();
	}

	private void displayCheckResults(String resultKey, JLabel label) {
		String checkResult = DataBaseUtils.getCheckResults(resultKey);
		if (checkResult.equals(DataBaseUtils.CHECK_RESULT_OK)) {
			label.setVisible(true);
			label.setForeground(OK_LABEL_COLOR);
			label.setText(checkResult);
		} else {
			label.setVisible(true);
			label.setForeground(ERR_LABEL_COLOR);
			label.setText("Реквизит" + label.getName() + " занят :(");
		}

	}

	/*
	 * private String getLabelResultName(JLabel label) { String str =
	 * label.getName(); String result = ""; switch (str) { case
	 * "isOgrnUniqueLabel": result = "ОГРН"; break; case "isKppUniqueLabel":
	 * result = "КПП"; break; case "isInnUniqueLabel": result = "ИНН"; break;
	 * case "isUserUniqueLabel": result = "ИНН"; break;
	 * 
	 * default: break; } return result;
	 * 
	 * }
	 */

	private void displayResultWindow() {
		registerResultFrame.setVisible(true);
		infoResultFullNameLabel.setText("Полное наименование: " +  orgFullName);
		infoResultLoginLabel.setText("Логин: " + userLogin);
		infoResultPasswordLabel.setText("Пароль: " + userPassword );
	}

	private void cancelButtonActionPerformed(ActionEvent evt) {
		dispose();
	}

	private void clearFormButonActionPerformed(ActionEvent evt) {
		clearTextFields(this);

	}

	void clearTextFields(Container container) {
		for (Component c : container.getComponents()) {
			if (c instanceof JTextField) {
				((JTextField) c).setText("");
			} else if (c instanceof Container) {
				clearTextFields((Container) c);
			}
		}
	}

	// Finds the size of the screen
	private static void findScreenSize() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		ScreenHeight = dim.height;
		ScreenWidth = dim.width;
	}

	// Centres the dialogue box within the screen
	private static void centreFrame(JFrame Frame, boolean Width) {
		findScreenSize();

		if (!Width) { // if you are not justifying on the X axis
			Frame.setLocation(Frame.getLocationOnScreen().x,
					((ScreenWidth / 2) - (Frame.getWidth() / 2)));
		} else {
			Frame.setLocation(((ScreenWidth / 2) - (Frame.getWidth() / 2)),
					((ScreenHeight / 2) - (Frame.getHeight() / 2)));
		}
	}

	private void fullNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void emailTextFieldActionPerformed(ActionEvent evt) {
	}

	private void ogrnTextFieldActionPerformed(ActionEvent evt) {
	}

	private void loginTextFieldActionPerformed(ActionEvent evt) {
	}

	private void firstNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void middleNameTextFieldActionPerformed(ActionEvent evt) {
	}

	private void innTextFieldActionPerformed(ActionEvent evt) {
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		/* Set the Nimbus look and feel */
		// <editor-fold defaultstate="collapsed"
		// desc=" Look and feel setting code (optional) ">
		/*
		 * If Nimbus (introduced in Java SE 6) is not available, stay with the
		 * default look and feel. For details see
		 * http://download.oracle.com/javase
		 * /tutorial/uiswing/lookandfeel/plaf.html
		 */
		try {
			UIManager.LookAndFeelInfo[] installedLookAndFeels = UIManager
					.getInstalledLookAndFeels();
			for (int idx = 0; idx < installedLookAndFeels.length; idx++)
				if ("Nimbus".equals(installedLookAndFeels[idx].getName())) {
					UIManager.setLookAndFeel(installedLookAndFeels[idx]
							.getClassName());
					break;
				}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(
					MainGuiRegistrationForm.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				mainFrame = new MainGuiRegistrationForm();
				mainFrame.setVisible(true);
				centreFrame(mainFrame, true);
			}
		});
	}

	// Variables declaration - do not modify
	private JButton cancelButton, checkOrgDataButton, checkUserDataButton,
			clearFormButon, registerButton, okSuccessButton;
	private JLabel emailLabel, firstNameLabel, fullNameLabel, innLabel,
			kppLabel, lastNameLabel, loginLabel, middleNameLabel, ogrnLabel,
			phoneLabel, isOgrnUniqueLabel, isKppUniqueLabel, isInnUniqueLabel,
			isLoginUniqueLabel, registrationSuccessLabel, infoResultLabel,
			infoResultFullNameLabel, infoResultLoginLabel,
			infoResultPasswordLabel;

	private JTextField emailTextField, firstNameTextField, fullNameTextField,
			innTextField, kppTextField, lastNameTextField, loginTextField,
			middleNameTextField, ogrnTextField, phoneTextField;

	private JPanel userDataPanel, orgDataPanel, registerResultPanel;

	private static String orgFullName, orgOgrn, orgInn, orgKpp, orgEmail,
			orgAddressId, orgPhone, orgWebsite, userLogin, userFirstName,
			userLastName, userMiddleName;

	// Дефолтные значения
	private static String userPassword = "11111";

	private static JFrame registerResultFrame;

	private static int ScreenWidth, ScreenHeight;
	
	private DataBaseUtils db = new DataBaseUtils();

	private static final Color OK_LABEL_COLOR = new Color(0, 0, 255);
	private static final Color ERR_LABEL_COLOR = new Color(255, 51, 51);
	// End of variables declaration

}
